import {createRouter, createWebHistory} from 'vue-router'
import storage from "../utils/storage";

import Gin from '../views/bingo.vue'
import Login from '../views/Login.vue'
import Base from '../views/Base.vue'
import ShowCenter from '../views/ShowCenter.vue'
import Host from '../views/Host.vue'
import Console from '../views/Console.vue'
import MultiExec from '../views/MultiExec.vue'
import TemplateManage from '../views/TemplateManage.vue'
import Auth from '../views/auth.vue'
import Role from '../views/role.vue'
import Permission from '../views/permission.vue'
import Menu from '../views/menu.vue'
import Sys from '../views/sys.vue'


const routes = [
    {
        meta: {
            title: 'uric自动化运维平台'
        },
        path: '/',
        alias: '/', // 给当前路径起一个别名
        name: 'Base',
        component: Base, // 快捷键：Alt+Enter快速导包,
        children: [
            {
                path: '',
                name: 'Index',
                component: Gin,
                meta: {
                    title: "展示中心",
                    authorization: true
                },
            },
            {
                path: 'personal',
                name: 'ShowCenter',
                component: ShowCenter,
                meta: {
                    title: "个人中心",
                    // authorization: true
                },
            },
            {
                path: 'host',
                name: 'Host',
                component: Host,
                meta: {
                    title: "主机管理",
                    authorization: true
                },
            },
            {
                meta: {
                    title: '远程主机管理',
                    authentication: true
                },
                path: 'host/console/:id', // :id 就是当前点击的主机信息的ID主键
                name: 'Console',
                component: Console
            },
            {
                meta: {
                    title: '批量任务',
                    authenticate: true
                },
                path: 'work/multi_exec',
                name: 'MultiExec',
                component: MultiExec
            },
            {
                meta: {
                    title: '指令模板',
                    authenticate: true
                },
                path: 'template_manage',
                name: 'TemplateManage',
                component: TemplateManage
            },
            {
                meta: {
                    title: '账户管理',
                    authenticate: true
                },
                path: '/auth/user',
                name: '账户管理',
                component: Auth
            },
            {
                meta: {
                    title: '角色管理',
                    authenticate: true
                },
                path: '/auth/role',
                name: '角色管理',
                component: Role
            },
            {
                meta: {
                    title: '权限管理',
                    authenticate: true
                },
                path: '/auth/permission',
                name: '权限管理',
                component: Permission
            },
            {
                meta: {
                    title: '菜单管理',
                    authenticate: true
                },
                path: '/auth/menu',
                name: '菜单管理',
                component: Menu
            },
            {
                meta: {
                    title: '系统设置',
                    authenticate: true
                },
                path: '/sys',
                name: '系统设置',
                component: Sys
            },
        ],
    },
    {
        meta: {
            title: '账户登陆',
            authorization: false
        },
        path: '/login',
        name: 'Login',
        component: Login // 快捷键：Alt+Enter快速导包，
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


// 导航守卫
router.beforeEach((to, from, next) => {
    document.title = to.meta.title
    // 登录状态验证
    if (to.meta.authorization && !storage.getUserInfo()) {
        next({"name": "Login"})
    } else {
        next()
    }
})

// 暴露路由对象
export default router